﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CF.Web.Default" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>自动生成系统</title>
        
    <style type="text/css">
        .x-grid3-cell-inner {
            font-family : "segoe ui",tahoma, arial, sans-serif;
        }
         
        .x-grid-group-hd div {
            font-family : "segoe ui",tahoma, arial, sans-serif;
        }
         
        .x-grid3-hd-inner {
            font-family : "segoe ui",tahoma, arial, sans-serif;
            font-size   : 12px;
        }
         
        .x-grid3-body .x-grid3-td-Cost {
            background-color : #f1f2f4;
        }
         
        .x-grid3-summary-row .x-grid3-td-Cost {
            background-color : #e1e2e4;
        }    
         
        .total-field{
            background-color : #fff;
            font-weight      : bold !important;                       
            color            : #000;
            border           : solid 1px silver;
            padding          : 2px;
            margin-right     : 5px;
        } 
    </style>
     
    <script type="text/javascript">
        var updateTotal = function (grid) {
            var fbar = grid.getBottomToolbar(),
                column,
                field,
                width,
                data = {},
                c,
                cs = grid.view.getColumnData();

            for (var j = 0, jlen = grid.store.getCount(); j < jlen; j++) {
                var r = grid.store.getAt(j);

                for (var i = 0, len = cs.length; i < len; i++) {
                    c = cs[i];
                    column = grid.getColumnModel().columns[i];

                    if (column.summaryType) {
                        data[c.name] = Ext.grid.GroupSummary.Calculations[column.summaryType](data[c.name] || 0, r, c.name, data);
                    }
                }
            }

            for (var i = 0; i < grid.getColumnModel().columns.length; i++) {
                column = grid.getColumnModel().columns[i];

                if (column.dataIndex != grid.store.groupField) {
                    field = fbar.findBy(function (item) {
                        return item.dataIndex === column.dataIndex;
                    })[0];

                    c = cs[i];
                    fbar.remove(field, false);
                    fbar.insert(i, field);
                    width = grid.getColumnModel().getColumnWidth(i);
                    field.setWidth(width - 5);
                    field.setValue((column.summaryRenderer || c.renderer)(data[c.name], {}, {}, 0, i, grid.store));
                }
            }

            fbar.doLayout();
        }

        var startEditing = function (e) {
            if (e.getKey() === e.ENTER) {
                var grid = GridPanel1,
                    record = grid.getSelectionModel().getSelected(),
                    index = grid.store.indexOf(record);

                grid.startEditing(index, 1);
            }
        };
        // helper
        var objToString = function (o) {
            var s = '{\n';
            for (var p in o)
                s += '    "' + p + '": "' + o[p] + '"\n';
            return s + '}';
        };

        var afterEdit = function (e) {
            /*
            Properties of 'e' include:
            e.grid - This grid
            e.record - The record being edited
            e.field - The field name being edited
            e.value - The value being set
            e.originalValue - The original value for the field, before the edit.
            e.row - The grid row index
            e.column - The grid column index
            */
            //alert(objToString(e.record.data));
            // Call DirectMethod
            Column.AfterEdit(e.record.data.EntityID, e.field, e.originalValue, e.value, e.record.data);
        };

        var prepare = function (grid, toolbar, groupId, records) {
            alert(objToString(records));
        };
    </script>
 
</head>
<body>
    <script type="text/javascript" src="Kwan.Base.js"></script>
    <script type="text/javascript" src="Kwan.Column.js"></script>
    <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Gray" DirectMethodNamespace="Column"/>

        <ext:Store ID="Store1" runat="server" OnRefreshData="RefreshDS" RemoteSort="True" GroupField="TableName">
            <Proxy>
                <ext:PageProxy />
            </Proxy>
            <Reader>
                <ext:JsonReader IDProperty="EntityID">
                    <Fields>
                        <ext:RecordField Name="EntityID" />
                        <ext:RecordField Name="ProjectName" />
                        <ext:RecordField Name="TableName" />
                        <ext:RecordField Name="NameCN" />
                        <ext:RecordField Name="NameEN" />
                        <ext:RecordField Name="Type" />
                        <ext:RecordField Name="Lenth" />
                        <ext:RecordField Name="IsQuery" />
                        <ext:RecordField Name="Field" />
                        <ext:RecordField Name="Memo" />
                    </Fields>
                </ext:JsonReader>
            </Reader>
            <AutoLoadParams>
                <ext:Parameter Name="start" Value="0" Mode="Raw" />
                <ext:Parameter Name="limit" Value="100" Mode="Raw"/>
            </AutoLoadParams>
            <SortInfo Field="TableName,NameEN" Direction="ASC" />
            <Listeners> 
                <LoadException Handler="Ext.MessageBox.alert('Load failed', response.statusText);" />                                     </Listeners>
        </ext:Store>

         <ext:Viewport ID="Viewport1" runat="server" Layout="Fit">
            <Items>
                <ext:GridPanel ID="GridPanel1" runat="server" StoreID="Store1" Frame="true" Title="项目数据库维护" AutoExpandColumn="NameEN" Icon="ApplicationViewColumns" TrackMouseOver="false" Width="800" Height="450" ClicksToEdit="2">
                    <ColumnModel ID="ColumnModel1" runat="server">
                        <Columns>
                            <ext:Column Header="表名" DataIndex="TableName" Width="130" />
                            <ext:Column ColumnID="NameEN" Header="列名" DataIndex="NameEN" Width="20">
                                <Editor>
                                    <ext:TextField runat="server" AllowBlank="false" />
                                </Editor>
                            </ext:Column>

                            <ext:Column ColumnID="NameCN" Header="中文名称" DataIndex="NameCN" Width="20">
                                <Editor>
                                    <ext:TextField runat="server" />
                                </Editor>
                            </ext:Column>
                     
                            <ext:Column ColumnID="Type" Width="25" Header="数据类型" DataIndex="Type">
                                <Editor>
                                    <ext:ComboBox runat="server" Editable="False">
                                        <Items>
                                            <ext:ListItem Text="int" />
                                            <ext:ListItem Text="long" />
                                            <ext:ListItem Text="float" />
                                            <ext:ListItem Text="double" />
                                            <ext:ListItem Text="string" />
                                            <ext:ListItem Text="bool" />
                                            <ext:ListItem Text="DateTime" />
                                        </Items>
                                    </ext:ComboBox>
                                </Editor>
                            </ext:Column>
 
                            <ext:Column Width="20" ColumnID="Lenth" Header="长度" DataIndex="Lenth">
                                <Editor>
                                    <ext:TextField ID="TextField2" runat="server" />
                                </Editor>
                            </ext:Column>

                            <ext:Column ColumnID="Field" Width="25" Header="文本类型" DataIndex="Field">
                                <Editor>
                                    <ext:ComboBox runat="server" Editable="False">
                                        <Items>
                                            <ext:ListItem Text="TextField" />
                                            <ext:ListItem Text="TextArea" />
                                            <ext:ListItem Text="DateField" />
                                            <ext:ListItem Text="HtmlEditor" />
                                        </Items>
                                    </ext:ComboBox>
                                </Editor>
                            </ext:Column>

                            <ext:Column ColumnID="IsQuery" Width="25" Header="查询字段" DataIndex="IsQuery">
                                <Editor>
                                    <ext:ComboBox runat="server" Editable="False">
                                        <Items>
                                            <ext:ListItem Text="True" />
                                            <ext:ListItem Text="False" />
                                        </Items>
                                    </ext:ComboBox>
                                </Editor>
                            </ext:Column>
                     
                            <ext:Column Width="20"  ColumnID="Memo" Header="说明" DataIndex="Memo">
                                 <Editor>
                                    <ext:TextField runat="server" AllowBlank="false" />
                                </Editor>
                            </ext:Column>
                            <ext:CommandColumn Hidden="true">
                                <GroupCommands>
                                    <ext:GridCommand Icon="TableRow" CommandName="AddGroup">
                                        <ToolTip Title="添加" Text="添加" />
                                    </ext:GridCommand>
                                    <ext:CommandFill />
                                </GroupCommands>
                            </ext:CommandColumn>
                        </Columns>
                        <Listeners>
                            <%--<WidthChange Handler="updateTotal(#{GridPanel1});" />--%>
                        </Listeners>
                    </ColumnModel>
                    <Listeners>
                        <%--<ColumnResize Handler="updateTotal(#{GridPanel1});" />
                        <AfterRender Handler="updateTotal(#{GridPanel1});" Delay="100" />--%>
                        <GroupCommand Handler="if(command === 'AddGroup'){ this.getSelectionModel().selectRecords(records, true); return;}" />
                        <KeyDown Fn="startEditing" />
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                    <SelectionModel>
                        <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" RowSpan="2" />
                    </SelectionModel>
                    <View>
                        <ext:GroupingView  
                            ID="GroupingView1"
                            HideGroupedColumn="true"
                            runat="server" 
                            ForceFit="true"
                            StartCollapsed="true"
                            GroupTextTpl='<span id="ColorCode-{[values.rs[0].data.ColorCode]}"></span>{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
                            EnableRowBody="true">
                            
                        </ext:GroupingView>
                    </View>
                    <TopBar>
                        <ext:Toolbar ID="Toolbar1" runat="server">
                            <Items>
                                <ext:Hidden ID="txtFilter" runat="server" AutoDataBind="true">
                                    <Listeners>
                                        <Change Handler="Store1.reload();" Delay="30" />
                                    </Listeners>
                                </ext:Hidden>
                                <ext:TriggerField ID="FilterProjectName" runat="server" EmptyText=" 搜索项目.." EnableKeyEvents="true">
                                    <Listeners>
                                        <%--<KeyDown Buffer="200" Handler="if(!Ext.isEmpty(this.getValue())) { this.triggers[0].show(); #{txtFilter}.setValue('TableName='+#{FilterTableName}.getValue() + '&ProjectName=' + this.getValue());}" />--%>
                                        <TriggerClick Handler="if(index == 0) { #{FilterProjectName}.reset(); #{txtFilter}.setValue(''); this.triggers[0].hide(); }" />
                                    </Listeners>
                                    <Triggers>
                                        <ext:FieldTrigger Icon="Clear" HideTrigger="true" />
                                    </Triggers>
                                </ext:TriggerField>

                                <ext:ToolbarSeparator ID="ToolbarSeparator1" runat="server" />

                                <ext:TriggerField ID="FilterTableName" runat="server" EmptyText=" 搜索表.." EnableKeyEvents="true">
                                    <Listeners>
                                        <%--<KeyDown Buffer="200" Handler="if(!Ext.isEmpty(this.getValue())) { this.triggers[0].show(); #{txtFilter}.setValue('ProjectName='+#{FilterProjectName}.getValue() + '&TableName=' + this.getValue());}" />--%>
                                        <TriggerClick Handler="if (index == 1) {Store1.reload(); } if(index == 0) { #{FilterTableName}.reset(); #{txtFilter}.setValue(''); this.triggers[0].hide(); }" />
                                    </Listeners>
                                    <Triggers>
                                        <ext:FieldTrigger Icon="Clear" HideTrigger="true" Qtip="清空" />
                                        <ext:FieldTrigger Icon="Search" Qtip="搜索"/>
                                    </Triggers>
                                </ext:TriggerField>

                                <ext:ToolbarFill ID="ToolbarFill1" runat="server" />

                                <ext:Button ID="BtnAddInfo" runat="server" Text="添加" Icon="Add">
                                    <DirectEvents>
                                        <Click OnEvent="BtnAddInfo_Click" />
                                    </DirectEvents>
                                </ext:Button>
                                <ext:Button ID="BtnDeleteInfo" runat="server" Text="批量删除" Icon="Exclamation">
                                    <Listeners>
                                        <Click Handler="Kwan.CheckDelete(#{GridPanel1}.getRowsValues({selectedOnly:true}));" />
                                    </Listeners>
                                </ext:Button>
                                <ext:Button ID="BtnCreateFile" runat="server" Text="生成文件" Icon="Add">
                                    <Listeners>
                                        <Click Handler="if(#{FilterProjectName}.getValue().length<=0){Kwan.showNotify('系统提示','请填写项目名称！');return false;}Kwan.CreateFile(Ext.encode(#{GridPanel1}.getRowsValues({selectedOnly:true})));" />
                                    </Listeners>
                                </ext:Button>
                            </Items>
                        </ext:Toolbar>
                    </TopBar>
                    <LoadMask ShowMask="true" />
                    <SaveMask ShowMask="true" />
                    <BottomBar>
                        <ext:PagingToolbar ID="PagingToolBar1" runat="server" PageSize="100">
                            <Items>
                                <ext:Label ID="Label1" runat="server" Text="每页显示:" />
                                <ext:ToolbarSpacer ID="ToolbarSpacer1" runat="server" Width="10" />
                                <ext:ComboBox ID="ComboBox3" Editable="false" runat="server" Width="80">
                                    <Items>
                                        <ext:ListItem Text="10" />
                                        <ext:ListItem Text="20" />
                                        <ext:ListItem Text="50" />
                                        <ext:ListItem Text="100" />
                                    </Items>
                                    <SelectedItem Value="100" />
                                    <Listeners>
                                        <Select Handler="#{PagingToolBar1}.pageSize = parseInt(this.getValue()); #{PagingToolBar1}.doLoad();" />
                                    </Listeners>
                                </ext:ComboBox>
                            </Items>
                        </ext:PagingToolbar>
                    </BottomBar>
                    <%--<Plugins>
                        <ext:GroupingSummary runat="server">
                            <Calculations>
                                <ext:JFunction Name="totalCost" Handler="return v + (record.data.Lenth * record.data.Memo);" />
                            </Calculations>
                        </ext:GroupingSummary>
                    </Plugins>--%>
                </ext:GridPanel>
            </Items>
        </ext:Viewport>

        <ext:Window ID="EditWindow" runat="server" Icon="Group" Title="编辑数据" Width="550" Height="400" AutoShow="false" Modal="false" Collapsible="true" Hidden="true" Layout="Fit" LabelWidth="70">
            <Items>
                <ext:TabPanel ID="TabPanel1" runat="server" ActiveTabIndex="0" Border="false" DeferredRender="false">
                    <Items>
                        <ext:FormPanel ID="ColumnForm" MonitorValid="true" runat="server" Title="编辑数据" Icon="ChartOrganisation" Padding="5" Layout="Form" LabelAlign="Right" LabelWidth="70" MonitorResize="true">
                            <Items>
                                <ext:Hidden ID="txtID" runat="server" />
                                <ext:TextField ID="txtProjectName" FieldLabel="项目名称" AnchorHorizontal="98%" AllowBlank="false" runat="server" />
                                <ext:TextField ID="txtTableName" FieldLabel="表格名称" AnchorHorizontal="98%" AllowBlank="false" runat="server" />
                                <ext:TextField ID="txtNameEN" FieldLabel="字段名称" AnchorHorizontal="98%" AllowBlank="false" runat="server" />
                                <ext:TextField ID="txtNameCN" FieldLabel="中文名称" AnchorHorizontal="98%" runat="server" />
                                <ext:ComboBox ID="txtType" Editable="false" FieldLabel="字段类型" EmptyText="请选择..." AnchorHorizontal="30%" AllowBlank="false" runat="server">
                                    <Items>
                                        <ext:ListItem Text="int" />
                                        <ext:ListItem Text="string" />
                                        <ext:ListItem Text="bool" />
                                        <ext:ListItem Text="DateTime" />
                                    </Items>
                                    <SelectedItem Value="string" />
                                </ext:ComboBox>
                                <ext:TextField ID="txtLength" FieldLabel="字段长度" AnchorHorizontal="98%" runat="server" />
                                <ext:CheckboxGroup ID="CheckboxGroup1" runat="server" FieldLabel="选项" AnchorHorizontal="98%">
                                    <Items>
                                        <ext:Checkbox ID="txtIsQuery" BoxLabel="查询字段" runat="server" />
                                        <%--<ext:Checkbox ID="txtScale" FieldLabel="显示" runat="server" />--%>
                                    </Items>
                                </ext:CheckboxGroup>
                                <ext:RadioGroup ID="txtField" FieldLabel="文本类型" runat="server">
                                <Items>
                                    <ext:Radio BoxLabel="TextField" runat="server" Checked="true" />
                                    <ext:Radio BoxLabel="TextArea" runat="server" />
                                    <ext:Radio BoxLabel="DateField" runat="server" />
                                    <ext:Radio BoxLabel="HtmlEditor" runat="server" />
                                </Items>
                                </ext:RadioGroup>
                                <%--<ext:ComboBox ID="txtIsQuery" Editable="false" FieldLabel="查询字段" EmptyText="请选择..." AnchorHorizontal="30%" AllowBlank="false" runat="server">
                                    <Items>
                                        <ext:ListItem Text="是" />
                                        <ext:ListItem Text="否" />
                                    </Items>
                                    <SelectedItem Value="是" />
                                </ext:ComboBox>--%>
                                <ext:TextArea ID="txtMemo" FieldLabel="字段描述" AnchorHorizontal="98%" runat="server">
                                </ext:TextArea>
                            </Items>
                            <Listeners>
                                <ClientValidation Handler="#{SaveButton}.setDisabled(!valid);" />
                            </Listeners>
                        </ext:FormPanel>
                    </Items>
                </ext:TabPanel>
            </Items>
            <Buttons>
                <ext:Button ID="SaveButton" runat="server" Text="保存" Icon="Disk">
                    <DirectEvents>
                        <Click OnEvent="SaveEmployee" Failure="Ext.MessageBox.alert('出错啦！', '请不要尝试非法操作！');">
                            <EventMask Target="CustomTarget" CustomTarget="={#{EditWindow}.body}" ShowMask="True" />
                        </Click>
                    </DirectEvents>
                </ext:Button>
                <ext:Button ID="CancelButton" runat="server" Text="返回" Icon="Cancel">
                    <Listeners>
                        <Click Handler="#{EditWindow}.hide(null);" />
                    </Listeners>
                </ext:Button>
            </Buttons>
        </ext:Window>
    </form>
  </body>
</html>